#!/bin/bash
j_script_dir=`dirname $0`
j_jenny_root_dir=`(cd $j_script_dir/..; pwd)`

. $j_jenny_root_dir/common/function

usage() {
	echo "Usage: $0 [options]..."
	echo "options:"
	echo "	-d <target>		make given target"
	exit 0
}
if [ "$1" = "-h" ]; then 
	usage
fi

detect_cmake j_cmake
j_code_signing=NO
j_code_sign_identity=

parse_args $@

j_defines="-DPROJECT_FILE=$j_config_path -DCMAKE_BUILD_TYPE=$j_config -DJ_TARGET_PLATFORM=$j_platform  -DJ_ARCHITECTURE=$j_architecture -DJ_TOOLSET=$j_toolset -DJ_TOOLSET_VER=$j_toolset_ver"

output=`$j_cmake $j_defines -P $j_jenny_root_dir/cmake/prepare.cmake`
for i in $output ; do eval ${i##--}; done

[ -d $j_generated_project_dir ] || die "Failed to locate generated project dir: $j_generated_project_dir"

cd $j_generated_project_dir

get_cpu_count j_cpu_count

if [ $j_toolset = "xcode" ]; then
	$j_cmake --build $j_generated_project_dir --config $j_config $j_target -- -jobs $j_cpu_count CODE_SIGN_IDENTITY="$j_code_sign_identity" CODE_SIGNING_REQUIRED=$j_code_signing || die "Build failed"
else
	make -j $j_cpu_count || die "Build failed"
fi

